namespace JQuery.WebKit
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;

    [TypeConverter(typeof(ExpandableObjectConverter)), AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class WebGridColumn : BaseItem
    {
        private WebGridEditClientSideValidatorCollection _editClientSideValidatorCollection;
        private WebGridEditFieldAttributeCollection _editFieldAttributeCollection;
        private WebGridColumnFormatterCollection _formatterCollection;

        internal virtual string FormatDataValue(object dataValue, bool encode)
        {
            if (this.IsNull(dataValue))
            {
                return this.NullDisplayText;
            }
            string s = dataValue.ToString();
            string dataFormatString = this.DataFormatString;
            int length = s.Length;
            if (!this.HtmlEncodeFormatString)
            {
                if ((length > 0) && encode)
                {
                    s = HttpUtility.HtmlEncode(s);
                }
                if ((length == 0) && this.ConvertEmptyStringToNull)
                {
                    return this.NullDisplayText;
                }
                if (dataFormatString.Length == 0)
                {
                    return s;
                }
                if (encode)
                {
                    return string.Format(CultureInfo.CurrentCulture, dataFormatString, new object[] { s });
                }
                return string.Format(CultureInfo.CurrentCulture, dataFormatString, new object[] { dataValue });
            }
            if ((length == 0) && this.ConvertEmptyStringToNull)
            {
                return this.NullDisplayText;
            }
            if (!string.IsNullOrEmpty(dataFormatString))
            {
                s = string.Format(CultureInfo.CurrentCulture, dataFormatString, new object[] { dataValue });
            }
            if (!string.IsNullOrEmpty(s) && encode)
            {
                s = HttpUtility.HtmlEncode(s);
            }
            return s;
        }

        internal bool IsNull(object value)
        {
            if ((value != null) && !Convert.IsDBNull(value))
            {
                return false;
            }
            return true;
        }

        [DefaultValue(true), Description("Determines if empty string should be converted to null."), Category("Functionality")]
        public bool ConvertEmptyStringToNull
        {
            get
            {
                if (base.ViewState["ConvertEmptyStringToNull"] != null)
                {
                    return (bool) base.ViewState["ConvertEmptyStringToNull"];
                }
                return true;
            }
            set
            {
                base.ViewState["ConvertEmptyStringToNull"] = value;
            }
        }

        [Category("Appearance"), DefaultValue(""), Description("Custom CssClass to apply to column cells.")]
        public string CssClass
        {
            get
            {
                if (base.ViewState["CssClass"] != null)
                {
                    return (string) base.ViewState["CssClass"];
                }
                return "";
            }
            set
            {
                base.ViewState["CssClass"] = value;
            }
        }

        [Category("Behavior"), Description("The field from the datasource mapped to this column."), DefaultValue("")]
        public string DataField
        {
            get
            {
                if (base.ViewState["DataField"] != null)
                {
                    return (string) base.ViewState["DataField"];
                }
                return "";
            }
            set
            {
                base.ViewState["DataField"] = value;
            }
        }

        [Description("The format applied to the column value for displaying. Uses the standard String.Format syntax."), DefaultValue(""), Category("Behavior")]
        public string DataFormatString
        {
            get
            {
                if (base.ViewState["DataFormatString"] != null)
                {
                    return (string) base.ViewState["DataFormatString"];
                }
                return "";
            }
            set
            {
                base.ViewState["DataFormatString"] = value;
            }
        }

        [DefaultValue(false), Description("Determines if the column is editable. Default is false."), Category("Editing")]
        public bool Editable
        {
            get
            {
                return ((base.ViewState["Editable"] != null) && ((bool) base.ViewState["Editable"]));
            }
            set
            {
                base.ViewState["Editable"] = value;
            }
        }

        [Description("Shows edit/save/cancel icons in the columns and forces the row in the respective edit actions based on end-user selection"), Category("Editing"), MergableProperty(false), DefaultValue(false)]
        public bool EditActionIconsColumn
        {
            get
            {
                return ((base.ViewState["EditActionIconsColumn"] != null) && ((bool) base.ViewState["EditActionIconsColumn"]));
            }
            set
            {
                base.ViewState["EditActionIconsColumn"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty), Description("The client-side validators that will be applied to the cell value upon editing."), MergableProperty(false), DefaultValue((string) null), Category("Editing")]
        public WebGridEditClientSideValidatorCollection EditClientSideValidators
        {
            get
            {
                if (this._editClientSideValidatorCollection == null)
                {
                    this._editClientSideValidatorCollection = new WebGridEditClientSideValidatorCollection();
                }
                return this._editClientSideValidatorCollection;
            }
        }

        [DefaultValue(0), Description("The column in which the edit field for this column will be shown in the Edit Dialog"), Category("Editing")]
        public int EditDialogColumnPosition
        {
            get
            {
                if (base.ViewState["EditDialogColumnPosition"] != null)
                {
                    return (int) base.ViewState["EditDialogColumnPosition"];
                }
                return 0;
            }
            set
            {
                base.ViewState["EditDialogColumnPosition"] = value;
            }
        }

        [Category("Editing"), DefaultValue(""), Description("Text shown before the editing field in the Edit Dialog. Default is none ('')")]
        public string EditDialogFieldPrefix
        {
            get
            {
                if (base.ViewState["EditDialogFieldPrefix"] != null)
                {
                    return (string) base.ViewState["EditDialogFieldPrefix"];
                }
                return "";
            }
            set
            {
                base.ViewState["EditDialogFieldPrefix"] = value;
            }
        }

        [Description("Text shown after the editing field in the Edit Dialog. Default is none ('')."), Category("Editing"), DefaultValue("")]
        public string EditDialogFieldSuffix
        {
            get
            {
                if (base.ViewState["EditDialogFieldSuffix"] != null)
                {
                    return (string) base.ViewState["EditDialogFieldSuffix"];
                }
                return "";
            }
            set
            {
                base.ViewState["EditDialogFieldSuffix"] = value;
            }
        }

        [DefaultValue(""), Category("Editing"), Description("The label of the editing field in edit dialog. Default to the column DataField if not set")]
        public string EditDialogLabel
        {
            get
            {
                if (base.ViewState["EditDialogLabel"] != null)
                {
                    return (string) base.ViewState["EditDialogLabel"];
                }
                return "";
            }
            set
            {
                base.ViewState["EditDialogLabel"] = value;
            }
        }

        [Category("Editing"), DefaultValue(0), Description("The row in which the edit field for this column will be shown in the Edit Dialog")]
        public int EditDialogRowPosition
        {
            get
            {
                if (base.ViewState["EditDialogRowPosition"] != null)
                {
                    return (int) base.ViewState["EditDialogRowPosition"];
                }
                return 0;
            }
            set
            {
                base.ViewState["EditDialogRowPosition"] = value;
            }
        }

        [MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), Description("The HTML attributes (name/value pairs) that will be applied to the edit field of the respective column."), DefaultValue((string) null), Category("Editing")]
        public WebGridEditFieldAttributeCollection EditFieldAttributes
        {
            get
            {
                if (this._editFieldAttributeCollection == null)
                {
                    this._editFieldAttributeCollection = new WebGridEditFieldAttributeCollection();
                }
                return this._editFieldAttributeCollection;
            }
        }

        [DefaultValue(""), Category("Editing"), IDReferenceProperty, Description("The ID of the server-side asp:dropdownlist to be used for editing")]
        public string EditorControlID
        {
            get
            {
                if (base.ViewState["EditDropDownListControlID"] != null)
                {
                    return (string) base.ViewState["EditDropDownListControlID"];
                }
                return "";
            }
            set
            {
                base.ViewState["EditDropDownListControlID"] = value;
            }
        }

        [Description("The type of the editing field for this column - TextBox, TextArea, DropDown, CheckBox, etc"), Category("Editing"), DefaultValue(0)]
        public JQuery.WebKit.EditType EditType
        {
            get
            {
                if (base.ViewState["EditType"] != null)
                {
                    return (JQuery.WebKit.EditType) base.ViewState["EditType"];
                }
                return JQuery.WebKit.EditType.TextBox;
            }
            set
            {
                base.ViewState["EditType"] = value;
            }
        }

        [Category("Editing"), DefaultValue(""), Description("The list of values when EditType = DropDown. Name/value pairs separated with ':', e.g. 'TNT:TNT;DHL:DHL;UPS:UPS'")]
        public string EditValues
        {
            get
            {
                if (base.ViewState["EditValues"] != null)
                {
                    return (string) base.ViewState["EditValues"];
                }
                return "";
            }
            set
            {
                base.ViewState["EditValues"] = value;
            }
        }

        [DefaultValue(""), Category("Appearance"), Description("What to display in the column footer. AppearanceSettings.ShowFooter must be set to True for this to have effect.")]
        public string FooterValue
        {
            get
            {
                if (base.ViewState["FooterValue"] != null)
                {
                    return (string) base.ViewState["FooterValue"];
                }
                return "";
            }
            set
            {
                base.ViewState["FooterValue"] = value;
            }
        }

        [Description("The formatter (client-side template) that will be applied to cells of this column."), MergableProperty(false), DefaultValue((string) null), Category("Functionality"), PersistenceMode(PersistenceMode.InnerProperty)]
        public WebGridColumnFormatterCollection Formatter
        {
            get
            {
                if (this._formatterCollection == null)
                {
                    this._formatterCollection = new WebGridColumnFormatterCollection();
                }
                return this._formatterCollection;
            }
        }

        [DefaultValue(0), Category("Grouping"), Description("The grouping function applied for this column")]
        public JQuery.WebKit.GroupSummaryType GroupSummaryType
        {
            get
            {
                if (base.ViewState["GroupSummaryType"] != null)
                {
                    return (JQuery.WebKit.GroupSummaryType) base.ViewState["GroupSummaryType"];
                }
                return JQuery.WebKit.GroupSummaryType.None;
            }
            set
            {
                base.ViewState["GroupSummaryType"] = value;
            }
        }

        [DefaultValue("{0}"), Description("The template applied to the grouping."), Category("Grouping")]
        public string GroupTemplate
        {
            get
            {
                if (base.ViewState["GroupTemplate"] != null)
                {
                    return (string) base.ViewState["GroupTemplate"];
                }
                return "{0}";
            }
            set
            {
                base.ViewState["GroupTemplate"] = value;
            }
        }

        [DefaultValue(""), Description("The text of the column header. If empty (default) the DataField value is used"), Category("Appearance")]
        public string HeaderText
        {
            get
            {
                if (base.ViewState["HeaderText"] != null)
                {
                    return (string) base.ViewState["HeaderText"];
                }
                return "";
            }
            set
            {
                base.ViewState["HeaderText"] = value;
            }
        }

        [Description("Determines if the text in the column cell should be HtmlEncdoed. Default is true."), DefaultValue(true), Category("Functionality")]
        public bool HtmlEncode
        {
            get
            {
                if (base.ViewState["HtmlEncode"] != null)
                {
                    return (bool) base.ViewState["HtmlEncode"];
                }
                return true;
            }
            set
            {
                base.ViewState["HtmlEncode"] = value;
            }
        }

        [Description("Determines if the DataFormatString should be HtmlEncoded. Default is true."), DefaultValue(true), Category("Functionality")]
        public bool HtmlEncodeFormatString
        {
            get
            {
                if (base.ViewState["HtmlEncodeFormatString"] != null)
                {
                    return (bool) base.ViewState["HtmlEncodeFormatString"];
                }
                return true;
            }
            set
            {
                base.ViewState["HtmlEncodeFormatString"] = value;
            }
        }

        [Category("Functionality"), DefaultValue(""), Description("What to display if the cell value is null. Default is nothing ('')")]
        public string NullDisplayText
        {
            get
            {
                if (base.ViewState["NullDisplayText"] != null)
                {
                    return (string) base.ViewState["NullDisplayText"];
                }
                return "";
            }
            set
            {
                base.ViewState["NullDisplayText"] = value;
            }
        }

        [DefaultValue(false), Category("Functionality"), Description("Sets the column as a primary key")]
        public bool PrimaryKey
        {
            get
            {
                return ((base.ViewState["PrimaryKey"] != null) && ((bool) base.ViewState["PrimaryKey"]));
            }
            set
            {
                base.ViewState["PrimaryKey"] = value;
            }
        }

        [Category("Functionality"), Description("Controls if the columns can be resized. Default is true."), DefaultValue(true)]
        public bool Resizable
        {
            get
            {
                if (base.ViewState["Resizable"] != null)
                {
                    return (bool) base.ViewState["Resizable"];
                }
                return true;
            }
            set
            {
                base.ViewState["Resizable"] = value;
            }
        }

        [DefaultValue(true), Description("Determines if the column should be searchable. Default is true."), Category("Searching")]
        public bool Searchable
        {
            get
            {
                if (base.ViewState["Searchable"] != null)
                {
                    return (bool) base.ViewState["Searchable"];
                }
                return true;
            }
            set
            {
                base.ViewState["Searchable"] = value;
            }
        }

        [Description("The ID of the server-side asp:dropdownlist to be used for searching"), DefaultValue(""), Category("Searching"), IDReferenceProperty]
        public string SearchControlID
        {
            get
            {
                if (base.ViewState["SearchControlID"] != null)
                {
                    return (string) base.ViewState["SearchControlID"];
                }
                return "";
            }
            set
            {
                base.ViewState["SearchControlID"] = value;
            }
        }

        [DefaultValue(0), Description("The type of the column - used by the auto-search functionality of the grid."), Category("Searching")]
        public JQuery.WebKit.SearchDataType SearchDataType
        {
            get
            {
                if (base.ViewState["SearchDataType"] != null)
                {
                    return (JQuery.WebKit.SearchDataType) base.ViewState["SearchDataType"];
                }
                return JQuery.WebKit.SearchDataType.NotSet;
            }
            set
            {
                base.ViewState["SearchDataType"] = value;
            }
        }

        [Category("Searching"), Description("The default search function for this column when toolbar searching is enabled. Default is Contains."), DefaultValue(12)]
        public SearchOperation SearchToolBarOperation
        {
            get
            {
                if (base.ViewState["SearchToolBarOperation"] != null)
                {
                    return (SearchOperation) base.ViewState["SearchToolBarOperation"];
                }
                return SearchOperation.Contains;
            }
            set
            {
                base.ViewState["SearchToolBarOperation"] = value;
            }
        }

        [DefaultValue(0), Description("The type of the control to be used for searching - TextBox or DropDown"), Category("Searching")]
        public JQuery.WebKit.SearchType SearchType
        {
            get
            {
                if (base.ViewState["SearchType"] != null)
                {
                    return (JQuery.WebKit.SearchType) base.ViewState["SearchType"];
                }
                return JQuery.WebKit.SearchType.TextBox;
            }
            set
            {
                base.ViewState["SearchType"] = value;
            }
        }

        [DefaultValue(""), Description("The list of values when SearchType = DropDown. Name/value pairs separated with ':', e.g. 'TNT:TNT;DHL:DHL;UPS:UPS'"), Category("Searching")]
        public string SearchValues
        {
            get
            {
                if (base.ViewState["SearchValues"] != null)
                {
                    return (string) base.ViewState["SearchValues"];
                }
                return "";
            }
            set
            {
                base.ViewState["SearchValues"] = value;
            }
        }

        [Category("Functionality"), DefaultValue(true), Description("Controls if the hidden columns can be searched. Default is true.")]
        public bool SearchHidden
        {
            get
            {
                if (base.ViewState["SearchHidden"] != null)
                {
                    return (bool)base.ViewState["SearchHidden"];
                }
                return true;
            }
            set
            {
                base.ViewState["SearchHidden"] = value;
            }
        }

        [Category("Functionality"), DefaultValue(true), Description("Controls if the columns can be sorted by clicking on the header. Default is true.")]
        public bool Sortable
        {
            get
            {
                if (base.ViewState["Sortable"] != null)
                {
                    return (bool) base.ViewState["Sortable"];
                }
                return true;
            }
            set
            {
                base.ViewState["Sortable"] = value;
            }
        }

        [DefaultValue(0), Description("The alignment of the cell text in the column. Default is left"), Category("Appearance")]
        public JQuery.WebKit.TextAlign TextAlign
        {
            get
            {
                if (base.ViewState["TextAlign"] != null)
                {
                    return (JQuery.WebKit.TextAlign) base.ViewState["TextAlign"];
                }
                return JQuery.WebKit.TextAlign.Left;
            }
            set
            {
                base.ViewState["TextAlign"] = value;
            }
        }

        [Description("If the column should be visible. You may want to have column non-visible, but editable for example"), DefaultValue(true), Category("Appearance")]
        public bool Visible
        {
            get
            {
                if (base.ViewState["Visible"] != null)
                {
                    return (bool) base.ViewState["Visible"];
                }
                return true;
            }
            set
            {
                base.ViewState["Visible"] = value;
            }
        }

        [DefaultValue(150), Category("Appearance"), Description("The width of the column. Supports integers only. Default is 150.")]
        public int Width
        {
            get
            {
                if (base.ViewState["Width"] != null)
                {
                    return (int) base.ViewState["Width"];
                }
                return 150;
            }
            set
            {
                base.ViewState["Width"] = value;
            }
        }
    }
}

